package com.gin.no_gay_alliance.fragment.notice;

import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;

import androidx.annotation.NonNull;

import com.gin.no_gay_alliance.activity.TopicReadActivity;
import com.gin.no_gay_alliance.activity.UserCenterActivity;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.ItemNoticeReplyBinding;
import com.gin.no_gay_alliance.utils.ViewUtils;
import com.gin.no_gay_alliance.viewmodel.notice.ReplyNoticeModel;

import java.util.Locale;

/**
 * 提醒消息 - 回复
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/11 13:46
 **/
public class ReplyNoticeFragment extends BaseNoticeTabFragment<ItemNoticeReplyBinding, ReplyNoticeModel> {

    @Override
    protected ItemNoticeReplyBinding buildBinding(LayoutInflater inflater) {
        return ItemNoticeReplyBinding.inflate(inflater);
    }

    /**
     * 绑定数据
     */
    @Override
    protected void onBindViewHolder(BaseBindingRecyclerViewAdapter.ViewHolder<ItemNoticeReplyBinding> holder, @NonNull ItemNoticeReplyBinding b, int position, ReplyNoticeModel item) {
        b.tvContent.setText(item.description);
        b.tvTimestamp.setText(item.datetime);
        b.tvTopicTitle.setText(item.topicTitle);
        ViewUtils.setGone(item.read, b.tvNoRead);
    }

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        super.onCreateView();

        b.tvTips.setVisibility(View.VISIBLE);

    }

    /**
     * 点击事件统一处理
     *
     * @param v 被点击的View
     */
    @Override
    public void onClick(View v) {
        super.onClick(v);
        ReplyNoticeModel replyNotice = (ReplyNoticeModel) v.getTag();
        TopicReadActivity.startActivity(requireActivity(), null
                , replyNotice.topicId
                , replyNotice.page
                , replyNotice.replyId
                , replyNotice.page
        );

    }

    /**
     * 长按事件
     *
     * @param v 被点击的item
     * @return true if the callback consumed the long click, false otherwise.
     */
    @Override
    public boolean onLongClick(View v) {
        read(v);
        // 长按打开自己的回复
        ReplyNoticeModel replyNotice = (ReplyNoticeModel) v.getTag();
        // 如果有作者
        boolean hasAuthor = replyNotice.authorId != null && replyNotice.authorId > 0;
        // 如果有被回复id
        boolean hasRepliedId = replyNotice.repliedId != null && replyNotice.repliedId != 0;
        if (hasAuthor && hasRepliedId) {
            // 弹出菜单，选择
            PopupMenu popupMenu = new PopupMenu(requireContext(), v);
            MenuItem menuItemAuthorName = popupMenu.getMenu().add(String.format("用户: %s", replyNotice.authorName));
            MenuItem menuItemMyRepliedId = popupMenu.getMenu().add(String.format(Locale.CHINA, "我的回复 %d", replyNotice.repliedId));
            popupMenu.setOnMenuItemClickListener(item -> {
                if (item == menuItemAuthorName) {
                    // 打开用户中心
                    UserCenterActivity.startActivity(requireContext(), replyNotice.authorId);
                    return true;
                }
                if (item == menuItemMyRepliedId) {
                    // 打开我的回复
                    TopicReadActivity.startActivity(requireContext(), replyNotice.repliedId);
                    return true;
                }
                return false;
            });
            popupMenu.show();
            return true;
        } else if (hasRepliedId) {
            TopicReadActivity.startActivity(requireActivity(), replyNotice.repliedId);
            return true;
        }
        return true;
    }
}
